package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsManagerInfoMapper;
import cc.rengu.igas.bsps.common.dao.EventInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsManagerInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.EventInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.EventInfo;
import cc.rengu.igas.bsps.common.entity.ManagerInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.bean.EventInfoBean;
import cc.rengu.igas.bsps.facade.request.NeedProcEventListQueryRequest;
import cc.rengu.igas.bsps.facade.response.NeedProcEventListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 待处理工单列表查询
 * Created by 王成 on 2020/3/24
 */
public class NeedProcEventListQueryService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        NeedProcEventListQueryRequest needProcEventListQueryRequest = new NeedProcEventListQueryRequest();
        ConvertUtil.convertOutput(needProcEventListQueryRequest);
        return needProcEventListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        NeedProcEventListQueryRequest needProcEventListQueryRequest = (NeedProcEventListQueryRequest) request;
        NeedProcEventListQueryResponse needProcEventListQueryResponse = new NeedProcEventListQueryResponse();
        BeanUtil.beanCopy(needProcEventListQueryRequest, needProcEventListQueryResponse);
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(needProcEventListQueryRequest.getUserId()) || StringUtil.isEmptyOrNull(needProcEventListQueryRequest.getManagerId()) ||
                StringUtil.isEmptyOrNull(needProcEventListQueryRequest.getStartDate()) || StringUtil.isEmptyOrNull(needProcEventListQueryRequest.getEndDate())) {
            rglog.error("用户编号<{}>,拓展经理工号<{}>,开始日期<{}>,截止日期<{}>不能为空!", needProcEventListQueryRequest.getUserId(), needProcEventListQueryRequest.getManagerId(), needProcEventListQueryRequest.getStartDate(), needProcEventListQueryRequest.getEndDate());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 查询所有事件申报信息 */
        EventInfoMapper eventInfoMapper = new EventInfoMapperImpl();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(needProcEventListQueryRequest.getPageIndex());
        pageInfo.setPageSize(needProcEventListQueryRequest.getPageSize());
        EventInfo eventInfo = new EventInfo();
        eventInfo.setInstId(needProcEventListQueryRequest.getHeader().getInstId());
        eventInfo.setDeclareDate(needProcEventListQueryRequest.getStartDate());
        eventInfo.setRemark1(needProcEventListQueryRequest.getEndDate());
        eventInfo.setEventType(needProcEventListQueryRequest.getEventDeclareType());
        //根据用户id 查询对应拓展经理
        String userId = needProcEventListQueryRequest.getUserId();
        String channelId = needProcEventListQueryRequest.getHeader().getChanlId();
        ManagerInfo managerInfo = null;
        BspsManagerInfoMapper managerInfoMapper = new BspsManagerInfoMapperImpl();
        if ("APP".equals(channelId)){
            managerInfo = managerInfoMapper.selectBspsManagerInfoById(needProcEventListQueryRequest.getHeader().getInstId(), userId);
        }else {
            managerInfo = managerInfoMapper.selectBspsManagerInfoByOpenId(needProcEventListQueryRequest.getHeader().getInstId(), userId);
        }
        if (managerInfo == null) {
            rglog.error("拓展经理不存在<{}>", userId);
            throw new BizException(RespCodeEnum.GET_USER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_INFO_ERROR.getRespDesc());
        }
        eventInfo.setCurrentProcUser(managerInfo.getManagerId());
        //工单状态
        String eventStatus = needProcEventListQueryRequest.getEventStatus();
        if (StringUtil.isEmptyOrNull(eventStatus)) {
            eventInfo.setEventStatus("00");
        } else if ("99".equals(eventStatus)) {
        } else {
            eventInfo.setEventStatus(eventStatus);
        }

        PageResult pageResult = eventInfoMapper.selectEventInfoByDeclareDatePage(eventInfo, pageInfo);
        List<EventInfoBean> eventInfoList = new ArrayList<>();

        needProcEventListQueryResponse.setTotalPage(0);
        needProcEventListQueryResponse.setTotalRows(0);
        if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
            rglog.error("未查到数据");
        } else {
            if (!pageResult.getResult().isEmpty()) {
                for (Object object : pageResult.getResult()) {
                    EventInfoBean eventInfoBean = new EventInfoBean();
                    EventInfo eventInfob = (EventInfo) object;
                    BeanUtil.beanCopy(eventInfob, eventInfoBean);
                    eventInfoBean.setEventDeclareType(eventInfob.getEventType());
                    eventInfoBean.setLastOprTime(eventInfob.getLastProcTime());
                    eventInfoBean.setEventDeclareInfo(eventInfob.getRemark3());
                    /* 凭证影像信息 */
                    List<String> credentialImageInfo = new ArrayList<>();
                    if (eventInfob.getVoucherImagePath1() != null) {
                        credentialImageInfo.add(eventInfob.getVoucherImagePath1());
                    }
                    if (eventInfob.getVoucherImagePath2() != null) {
                        credentialImageInfo.add(eventInfob.getVoucherImagePath2());
                    }
                    if (eventInfob.getVoucherImagePath3() != null) {
                        credentialImageInfo.add(eventInfob.getVoucherImagePath3());
                    }
                    eventInfoBean.setCredentialImageInfo(credentialImageInfo);
                    eventInfoList.add(eventInfoBean);
                }
                needProcEventListQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                needProcEventListQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
                needProcEventListQueryResponse.setPageIndex(pageResult.getPageInfo().getPageIndex());
                needProcEventListQueryResponse.setPageSize(pageResult.getPageInfo().getPageSize());
                needProcEventListQueryResponse.setEventInfoList(eventInfoList);
            }
        }


        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(needProcEventListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        NeedProcEventListQueryResponse needProcEventListQueryResponse = (NeedProcEventListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(needProcEventListQueryResponse);
    }

    /**
     * 工单信息转换
     *
     * @param eventInfo         事件信息表流水
     * @param eventInfoBeanList 工单信息
     */
    private void convEventInfo(EventInfo eventInfo, List<EventInfoBean> eventInfoBeanList) {
        EventInfoBean eventInfoBean = new EventInfoBean();
        /* 机构标识 */
        eventInfoBean.setInstId(eventInfo.getInstId());
        /* 事件编号 */
        eventInfoBean.setEventId(eventInfo.getEventId());
        /* 事件申报类型 */
        eventInfoBean.setEventDeclareType(eventInfo.getEventType());
        /* 事件申报内容 */
        eventInfoBean.setEventDeclareInfo(eventInfo.getEventDesc());
        /* 申报人 */
        eventInfoBean.setDeclareId(eventInfo.getDeclareUser());
        /* 申报日期 */
        eventInfoBean.setDeclareDate(eventInfo.getDeclareDate());
        /* 申报时间 */
        eventInfoBean.setDeclareTime(eventInfo.getDeclareTime());
        /* 当前处理状态 */
        eventInfoBean.setEventStatus(eventInfo.getEventStatus());
        /* 处理结果描述 */
        eventInfoBean.setEventProcResult(eventInfo.getEventProcResult());
        /* 最后处理人 */
        eventInfoBean.setCurrentOprId(eventInfo.getCurrentProcUser());
        /* 最后处理时间 */
        eventInfoBean.setLastOprTime(eventInfo.getLastProcTime());
        /* 事件优先级 */
        eventInfoBean.setEventPriority(eventInfo.getEventPriority());
        /* 标题 */
        eventInfoBean.setEventTitle(eventInfo.getEventTitle());
        /* 凭证影像信息 */
        List<String> credentialImageInfo = new ArrayList<>();
        if (eventInfo.getVoucherImagePath1() != null) {
            credentialImageInfo.add(eventInfo.getVoucherImagePath1());
        }
        if (eventInfo.getVoucherImagePath2() != null) {
            credentialImageInfo.add(eventInfo.getVoucherImagePath2());
        }
        if (eventInfo.getVoucherImagePath3() != null) {
            credentialImageInfo.add(eventInfo.getVoucherImagePath3());
        }
        eventInfoBean.setCredentialImageInfo(credentialImageInfo);
        eventInfoBeanList.add(eventInfoBean);

    }
}
